var pg;
function addRole() {
    $('.modal-title').html("添加角色");
    showForm();
}
function showForm() {
    $("#role_name_div").removeClass("has-error");
    $("#role_sign_div").removeClass("has-error");
    $("#role_desc_div").removeClass("has-error");
    $("#role_name").val("");
    $("#role_sign").val("");
    $("#role_desc").val("");
    $("#role_id").val("");
    $('#myModal').modal();
}
//提交处理
function formSubmit() {
    $("#role_name_div").removeClass("has-error");
    $("#role_sign_div").removeClass("has-error");
    $("#role_desc_div").removeClass("has-error");
    var name, sign, desc, id;
    name = $.trim($("#role_name").val());
    if (!name || name == '') {
        $("#role_name_div").addClass("has-error");
        return;
    }
    sign = $.trim($("#role_sign").val());
    if (!sign || sign == '') {
        $("#role_sign_div").addClass("has-error");
        return;
    }
    desc = $.trim($("#role_desc").val());
    id = $.trim($("#role_id").val());
    $.ajax({
        url: "system/changeRole",
        type: "POST",
        async: false,
        timeout: 3000,
        data: {id: id, name: name, sign: sign, desc: desc},
        dataType: "json",
        success: function (data) {
            if (data) {
                if (data.code == 100) {
                    //getModule();
                    //showSuccess("保存成功!");
                    $('#myModal').modal('hide');
                    pg.loadData();
                } else {
                    alert(data.message);
                }
            } else {
                alert("保存失败!");
            }
        },
        error: function () {
            alert("保存失败!");
        }
    });
}
function updateRole() {
    var arr = pg.getChecked();
    if (arr.length == 1) {
        $('.modal-title').html("修改角色");
        showForm();
        var a = arr[0];
        $("#role_name").val(a.roleName);
        $("#role_sign").val(a.roleSign);
        $("#role_desc").val(a.description);
        $("#role_id").val(a.id);
    } else {
        showAlert("请选择一条记录!");
    }
}
function deleteRole() {
    var arr = pg.getCheckedValue();
    if (arr.length > 0) {
        $.ajax({
            url: "system/deleteRole",
            type: "POST",
            async: false,
            timeout: 3000,
            data: {ids: arr.toString()},
            dataType: "json",
            success: function (data) {
                if (data) {
                    if (data.code == 100) {
                        pg.loadData();
                    } else {
                        showAlert(data.message);
                    }
                } else {
                    showAlert("删除失败!");
                }
            },
            error: function () {
                showAlert("删除失败!");
            }
        });
    } else {
        showAlert("至少选择一条记录!");
    }
}
$(document).ready(function () {
    pg = $('#table').pagination({
        url: 'system/roledata',
        hasCk: true,
        column: [{
            name: '#',
            property: 'id'
        }, {
            name: '角色名',
            property: 'roleName'
        }, {
            name: '角色标识',
            property: 'roleSign'
        }, {
            name: '说明',
            property: 'description'
        }]
    });
    pg.loadData();
    $("#addRole").bind("click", addRole);
    $("#role_form").bind("click", formSubmit);
    $("#updateRole").bind("click", updateRole);
    $("#remove").bind("click", deleteRole);
    //$("#saveMenu").bind("click", saveModule);
});
